home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / EDITSDI.PAK / FILE.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  10KB  |  359 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: file.c
  9. //
  10. //  PURPOSE: File manipulation functions.
  11. //
  12. //  FUNCTIONS:
  13. //    CmdFileNew - Create a new text document.
  14. //    CmdFileSave - Save the current document.
  15. //    Open - Open a file based on a file name and readonly attribute.
  16. //    QuerySaveFile - Save the current document if necessary & the user agrees.
  17. //    SetNewBuffer - Create a new edit buffer.
  18. //    SaveFile - Save the text from the editor control to the current file.
  19. //    SaveAs - Save the edit text to the specified file.
  20. //
  21. //  COMMENTS:
  22. //
  23.  
  24.  
  25. #include <windows.h>            // required for all Windows applications
  26. #ifdef WIN16
  27. #include "win16ext.h"           // required only for win16 applications
  28. #endif
  29. #include "globals.h"            // prototypes specific to this application
  30. #include "resource.h"
  31.  
  32. void SetNewBuffer(HWND, HANDLE, LPSTR);
  33. BOOL SaveFile(HWND);
  34.  
  35. char szFName[256];
  36. static char szUntitled[] = "(untitled)";// default window title
  37.  
  38.  
  39. //
  40. //  FUNCTION: CmdFileNew(HWND, WORD, WORD, HWND)
  41. //
  42. //  PURPOSE: Create a new text document.
  43. //
  44. //  PARAMETERS:
  45. //    hwnd     - The window.
  46. //    wCommand - IDM_FILENEW (unused)
  47. //    wNotify  - Notification number (unused)
  48. //    hwndCtrl - NULL (unused)
  49. //
  50. //  RETURN VALUE:
  51. //    Always returns 0 - command handled.
  52. //
  53. //  COMMENTS:
  54. //
  55. //
  56.  
  57. #pragma argsused
  58. LRESULT CmdFileNew(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  59. {
  60.     char rgch[256];
  61.  
  62.     // If current file has been modified, query user about saving it.
  63.     if (!QuerySaveFile(hwnd))
  64.         return 0;
  65.  
  66.     // The MODIFY attribute of the edit window is set to FALSE to indicate
  67.     //   there have been no changes since the last file save.
  68.     SendMessage(GetEditWindow(), EM_SETMODIFY, FALSE, 0L);
  69.     GetFName()[0] = 0;
  70.  
  71.     // Update the edit buffer
  72.     wsprintf(rgch, "%s - %s", szAppName, szUntitled);
  73.     SetNewBuffer(hwnd, NULL, rgch);
  74.  
  75.     return 0;
  76. }
  77.  
  78. //
  79. //  FUNCTION: CmdFileSave(HWND, WORD, WORD, HWND)
  80. //
  81. //  PURPOSE: Save the current document.
  82. //
  83. //  PARAMETERS:
  84. //    hwnd     - The window.
  85. //    wCommand - IDM_FILESAVE (unused)
  86. //    wNotify  - Notification number (unused)
  87. //    hwndCtrl - NULL (unused)
  88. //
  89. //  RETURN VALUE:
  90. //    Always returns 0 - command handled.
  91. //
  92. //  COMMENTS:
  93. //
  94. //
  95.  
  96. #pragma argsused
  97. LRESULT CmdFileSave(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  98. {
  99.      // If there is no filename, use the saveas command to get one.
  100.     //   Otherwise, save the file using the current filename.
  101.     if (!GetFName()[0])
  102.     {
  103.         CmdFileSaveAs(hwnd, wCommand, 0, hwndCtrl);
  104.     }
  105.     else
  106.     {
  107.         // If the document has not been modified, don't bother saving it.
  108.         if (SendMessage(GetEditWindow(), EM_GETMODIFY, FALSE, 0L))
  109.             SaveFile(hwnd);
  110.     }
  111.  
  112.      return 0;
  113. }
  114.  
  115.  
  116. //
  117. //  FUNCTION: Open(LPSTR, BOOL, HWND)
  118. //
  119. //  PURPOSE: Open a file based on a file name and readonly attribute.
  120. //
  121. //  PARAMETERS:
  122. //    lsz - The full path name of the file to be opened.
  123. //    fReadOnly - Read only attribute of the file.
  124. //    hwnd - The window into which the file is being opened.
  125. //
  126. //  RETURN VALUE:
  127. //    NONE
  128. //
  129. //  COMMENTS:
  130. //
  131. //
  132.  
  133. VOID Open(LPSTR lsz, BOOL fReadOnly, HWND hwnd)
  134. {
  135.     HFILE   hfile;      // Hande to the file being opened.
  136.     HCURSOR hcursSave;  // The cursor replace by the hourglass during save.
  137.     LPVOID lpvEdit;     // The edit buffer
  138.     long   cchSize;     // The size of the edit buffer
  139.     UINT   cchRead;     // The number of bytes read from the file
  140.     HANDLE hszT;        // Handle to the edit buffer
  141.  
  142.     // Open the file and get its handle
  143.     hfile = _lopen(lsz, OF_READ | OF_SHARE_DENY_WRITE);
  144.     if (hfile == HFILE_ERROR)
  145.         return;
  146.  
  147.     // Allocate edit buffer to the size of the file + 1
  148.     cchSize = _llseek(hfile, 0L, 2);
  149.     _llseek(hfile, 0L, 0);
  150.     hszT = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, (UINT)cchSize + 1);
  151.  
  152.     if (!hszT)
  153.     {
  154.         MessageBox(hwnd,
  155.                    "Not enough memory.", 
  156.                    SZAPPNAME, 
  157.                    MB_OK | MB_ICONHAND);
  158.         return;
  159.     }
  160.     hcursSave = SetCursor(hcursHourGlass);
  161.     lpvEdit = LocalLock(hszT);
  162.  
  163.     cchRead = _lread(hfile, lpvEdit, (UINT)cchSize);
  164.     _lclose(hfile);
  165.  
  166.     // # bytes read must equal file size
  167.     if (cchRead == (UINT) HFILE_ERROR ||  cchRead != (UINT)cchSize)
  168.     {
  169.         char rgchT[256];
  170.  
  171.         wsprintf(rgchT, "Error reading %s.", lsz);
  172.         SetCursor(hcursSave);      // Remove the hourglass
  173.         MessageBox(hwnd, rgchT, SZAPPNAME, MB_OK | MB_ICONEXCLAMATION);
  174.     }
  175.     LocalUnlock(hszT);
  176.  
  177.     // Set up a new buffer and window title
  178.     {
  179.         char rgch[256];
  180.  
  181.         wsprintf(rgch, "%s - %s", szAppName, lsz);
  182.         SetNewBuffer(hwnd, hszT, rgch);
  183.     }
  184.  
  185.     SendMessage(GetEditWindow(), EM_SETREADONLY, fReadOnly, 0L);
  186.     SetCursor(hcursSave);
  187.     lstrcpy(GetFName(), lsz);
  188. }
  189.  
  190. //
  191. //  FUNCTION: QuerySaveFile(HWND)
  192. //
  193. //  PURPOSE: Save the current document if necessary & the user agrees.
  194. //
  195. //  PARAMETERS:
  196. //    hwnd - The document window.
  197. //
  198. //  RETURN VALUE:
  199. //    TRUE  - The file has been saved, or any changes can be thrown out.
  200. //    FALSE - The use has changed his/her mind, don't continue the
  201. //      operation that would throw away this document.
  202. //
  203. //  COMMENTS:
  204. //
  205. //
  206.  
  207. BOOL QuerySaveFile(HWND hwnd)
  208. {
  209.     int  idResponse;
  210.     char rgch[256];
  211.  
  212.     if (SendMessage(GetEditWindow(), EM_GETMODIFY, 0, 0L))
  213.     {
  214.         wsprintf(rgch, "Save current changes? %s", GetFName());
  215.         idResponse = MessageBox(hwnd, 
  216.                                 rgch, 
  217.                                 SZAPPNAME,  
  218.                                 MB_YESNOCANCEL | MB_ICONEXCLAMATION);
  219.  
  220.         if (idResponse == IDYES)            // Save changes
  221.         {
  222.             // Make sure there is a filename to save to
  223.             if (!GetFName()[0])
  224.                 return (BOOL)CmdFileSaveAs(hwnd, IDM_FILESAVEAS, 0, NULL);
  225.             else
  226.                 SaveFile(hwnd);
  227.         }
  228.         else if (idResponse == IDCANCEL)    // Cancel entire operation
  229.             return FALSE;
  230.     }
  231.  
  232.     return TRUE;                            // Proceed
  233. }
  234.  
  235.  
  236. //
  237. //  FUNCTION: SetNewBuffer(HWND, HANDLE, LPSTR)
  238. //
  239. //  PURPOSE: Create a new edit buffer.
  240. //
  241. //  PARAMETERS:
  242. //    hwnd    - Handle to the main window.
  243. //    hsz     - Local handle to the text (may be NULL).
  244. //    szTitle - Title to display for the user.
  245. //
  246. //  RETURN VALUE:
  247. //    None.
  248. //
  249. //  COMMENTS:
  250. //
  251.  
  252. void SetNewBuffer(HWND hwnd, HANDLE hsz, LPSTR szTitle)
  253. {
  254.     if (!hsz)                    // Allocates a buffer if none exists
  255.         hsz = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, 1);
  256.  
  257.     // Updates the buffer and displays new buffer
  258.     SetEditText(GetEditWindow(), hsz);
  259.  
  260.     SetWindowText(hwnd, szTitle);
  261.     SetFocus(GetEditWindow());
  262.     SendMessage(GetEditWindow(), EM_SETMODIFY, FALSE, 0L);
  263. }
  264.  
  265.  
  266. //
  267. //  FUNCTION: SaveFile(HWND)
  268. //
  269. //  PURPOSE: Save the text from the editor control to the current file.
  270. //
  271. //  PARAMETERS:
  272. //    hwnd - The main application window.
  273. //
  274. //  RETURN VALUE:
  275. //    TRUE - Sucessfully saved the file.
  276. //    FALSE - Unable to save the file.
  277. //
  278. //  COMMENTS:
  279. //
  280.  
  281. BOOL SaveFile(HWND hwnd) 
  282. {
  283.     BOOL  fSuccess;
  284.     int   IOStatus;  // result of a file write
  285.     char  rgch[256];
  286.     HFILE hfile;
  287.     OFSTRUCT ofs;
  288.     HCURSOR hcursSave;
  289.     LPVOID lpvEdit;
  290.  
  291.     hfile = OpenFile(GetFName(),  &ofs, OF_PROMPT | OF_CANCEL | OF_CREATE);
  292.     if (hfile == HFILE_ERROR)
  293.     {
  294.         // If the file can't be saved
  295.  
  296.         wsprintf(rgch, "Cannot write to %s.", GetFName());
  297.         MessageBox(hwnd, rgch, SZAPPNAME, MB_OK | MB_ICONEXCLAMATION);
  298.         return FALSE;
  299.     }
  300.  
  301.     lpvEdit = LockEditText(GetEditWindow());
  302.  
  303.     // Set the cursor to an hourglass during the file transfer
  304.  
  305.     hcursSave = SetCursor(hcursHourGlass);
  306.     IOStatus = _lwrite(hfile, lpvEdit, lstrlen(lpvEdit));
  307.     _lclose(hfile);
  308.     SetCursor(hcursSave);
  309.  
  310.     if (IOStatus != (int)lstrlen(lpvEdit))
  311.     {
  312.         wsprintf(rgch, "Error writing to %s.", GetFName());
  313.         MessageBox(hwnd, rgch, SZAPPNAME, MB_OK | MB_ICONHAND);
  314.         fSuccess = FALSE;
  315.     }
  316.     else
  317.     {
  318.         fSuccess = TRUE;    // Indicates the file was saved
  319.         SendMessage(GetEditWindow(), EM_SETMODIFY, FALSE, 0L);
  320.     }
  321.  
  322.     UnlockEditText(GetEditWindow());
  323.     return fSuccess;
  324. }
  325.  
  326.  
  327. //
  328. //  FUNCTION: SaveAs(LPSTR, HWND)
  329. //
  330. //  PURPOSE: Save the edit text to the specified file.
  331. //
  332. //  PARAMETERS:
  333. //    lsz  - The fully qualified path of the file to save.
  334. //    hwnd - The main application window.
  335. //
  336. //  RETURN VALUE:
  337. //    NONE
  338. //
  339. //  COMMENTS:
  340. //
  341. //
  342.  
  343. VOID SaveAs(LPSTR lsz, HWND hwnd)
  344. {
  345.     char rgch[256];
  346.  
  347.     lstrcpy(rgch, lsz);
  348.     lstrcpy(GetFName(), lsz);
  349.     if (SaveFile(hwnd))
  350.     {
  351.         char rgchTitle[256];
  352.  
  353.         wsprintf(rgchTitle, "%s - %s", szAppName, GetFName());
  354.         SetWindowText(hwnd, rgchTitle);
  355.     }
  356.     else
  357.         lstrcpy(GetFName(), rgch);
  358. }
  359.